//
//  HDSystemInfoScrollView.m
//  HDecoration
//
//  Created by 曹群 on 15/8/15.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDSystemInfoScrollView.h"
@interface HDSystemInfoScrollView ()
@property CGPoint activePoint;
@end

@implementation HDSystemInfoScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _showLogo=YES;
        [self addSubview:self.backgroundImage];
        self.contentSize = CGSizeMake(self.width, self.backgroundImage.height+20);
        [self registerForKeyboardNotifications];
    }
    return self;
}

- (void)setBackgroundImageHeight:(float)value
{
    _backgroundImageHeight = value;
    self.backgroundImage.height = _backgroundImageHeight;
    self.contentSize = CGSizeMake(self.width, self.backgroundImage.height+20);
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(textBeginEditing:)
                                                name:UITextViewTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(textBeginEditing:)
                                                name:UITextFieldTextDidBeginEditingNotification object:nil];
    
}

- (void)textBeginEditing:(NSNotification*)aNotification
{
    UIView* sender=[aNotification object];
    CGRect rect=sender.frame;
    rect.origin.y+=rect.size.height;
    self.activePoint=[[sender superview] convertPoint:rect.origin toView:self];
    self.activePoint=[self convertPoint:self.activePoint toView:self];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    kbSize.height+=30;
    
    float hiddenH=self.frame.size.height-(self.frame.size.height-kbSize.height);
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,hiddenH, 0.0);
    self.contentInset = contentInsets;
    self.scrollIndicatorInsets = contentInsets;
    float activeH=self.frame.size.height-self.activePoint.y;
    if(kbSize.height>activeH)
    {
        float dif=activeH-kbSize.height;
        CGPoint scrollPoint = CGPointMake(0.0,0-dif);
        [self setContentOffset:scrollPoint animated:YES];
    }
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [UIView animateWithDuration:0.3 animations:^{
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        self.contentInset = contentInsets;
        self.scrollIndicatorInsets = contentInsets;
    }];
}

-(void)setShowLogo:(BOOL)showLogo
{
    _showLogo=showLogo;
    if (!showLogo)
    {
       self.headerImage.image=[UIImage imageNamed:@"帮助头视图2"];
    }
}
- (UIImageView *)backgroundImage
{
    if (!_backgroundImage)
    {
        _backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, APPFRAME.size.width-20, (APPFRAME.size.width-20)*864/600.0)];
        UIImage *image = [UIImage imageNamed:@"系统帮助背景"];
        image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:_backgroundImage.height-10];
        _backgroundImage.image = image;
        _backgroundImage.layer.masksToBounds = YES;
        _backgroundImage.userInteractionEnabled = YES;
        [_backgroundImage addSubview:self.headerImage];
    }
    return _backgroundImage;
}

- (UIImageView *)headerImage
{
    if (!_headerImage)
    {
        _headerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width-20, (APPFRAME.size.width-20)*225/600)];
        _headerImage.image = [UIImage imageNamed:@"帮助头视图"];
        _headerImage.layer.masksToBounds = YES;
    }
    return _headerImage;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
